#include "AIManager.h"
#include "GamePlayState.h"

AIManager * AIManager::pInstance = nullptr;

AIManager::AIManager(void)
{
	this->actionTime = 2;
	this->actionTimer = 0;
	this->curChar = 0;
	moved = false;
	this->characters.clear();
	this->abilityEnum = -1;
	this->target = nullptr;
}


AIManager::~AIManager(void)
{
}

void AIManager::Update(float fElapsedTime)
{
	if(curChar == 0 && moved == false)
		GamePlayState::GetInstance()->StartActionWindow(MOVE2);

	actionTimer += fElapsedTime;

	if(this->characters[curChar]->IsDead())
	{
		curChar++;
		if((unsigned int)curChar >= characters.size())
		{
			curChar = 0;
			(*GamePlayState::GetInstance()->GetPlayerBool()) = true;
			GamePlayState::GetInstance()->SetWeatherTime(GamePlayState::GetInstance()->GetWeatherTime() + 1);

			GamePlayState::GetInstance()->ChangeTurnBool();

			for(unsigned int i = 0; i < (*GamePlayState::GetInstance()->GetCharacters()).size();i++)
			{
				(*GamePlayState::GetInstance()->GetCharacters())[i]->SetActed(false);
				(*GamePlayState::GetInstance()->GetCharacters())[i]->SetMoved(false);
				(*GamePlayState::GetInstance()->GetCharacters())[i]->UpdateStatusEffects();
			}
		}
	}

	if(!moved && actionTimer >= actionTime)
	{
		GamePlayState::GetInstance()->SetActiveTile(this->characters[this->curChar]->GetTile());
		GamePlayState::GetInstance()->SetAImoving(true);
		this->characters[this->curChar]->GetAI()->AIMove();
		moved = true;
		actionTimer = 0.0f;
		this->characters[this->curChar]->GetAI()->AIUseAction();
		GamePlayState::GetInstance()->SetAITargeting(true);
		GamePlayState::GetInstance()->SetAICharacters(target,this->characters[this->curChar]);
	}

	if(moved && actionTimer >= actionTime)
	{
		GamePlayState::GetInstance()->SetAITargeting(false);
		if(target != nullptr)
			this->characters[this->curChar]->GetAI()->AIUseAction(this->target,this->abilityEnum);
		moved = false;
		actionTimer = 0.0f;
		
		
		this->curChar++;
		
		if((unsigned int)curChar >= characters.size()-1)
			GamePlayState::GetInstance()->StartActionWindow(MOVE2);

		if(unsigned int(curChar) >= this->characters.size())
		{
			curChar = 0;
			(*GamePlayState::GetInstance()->GetPlayerBool()) = true;
			GamePlayState::GetInstance()->SetWeatherTime(GamePlayState::GetInstance()->GetWeatherTime() + 1);

			GamePlayState::GetInstance()->ChangeTurnBool();
			GamePlayState::GetInstance()->SetAImoving(false);
			

			for(unsigned int i = 0; i < (*GamePlayState::GetInstance()->GetCharacters()).size();i++)
			{
				(*GamePlayState::GetInstance()->GetCharacters())[i]->SetActed(false);
				(*GamePlayState::GetInstance()->GetCharacters())[i]->SetMoved(false);
				(*GamePlayState::GetInstance()->GetCharacters())[i]->UpdateStatusEffects();
			}

		}

	}
}

AIManager* AIManager::GetInstance()
{
	if(pInstance == nullptr)
		pInstance = new AIManager();

	return pInstance;
}

void AIManager::DeleteInstance()
{
	if(pInstance != nullptr)
	{
		delete pInstance;
		pInstance = nullptr;
	}
}